package UI;

import Logic.Account;
import Logic.SQLReader;
import UI.BaseModels.BCBSPanel;
import UI.BaseModels.HeaderLabel;
import UI.BaseModels.ReturnToMainMenuButton;

import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;

import threads.BaseRunnableBCBS;

/**
 * Contains all the buttons, labels, text fields and methods required for the user to edit his/her own information.
 */
public class EditUserSelfPanel extends BCBSPanel {

	private static final long serialVersionUID = -7558057897521821340L;
	
	private JTextField textFieldFirstNameValue;
	
	private JTextField textFieldLastNameValue;
	
	private JLabel lblEmailValue;
	
	private JLabel lblBalanceValue;
	
	private JLabel lblUserIDValue;
	
	private JButton btnRefresh;
	
	private JButton btnUpdate;

	public EditUserSelfPanel() {

		HeaderLabel header = new HeaderLabel("Edit Profile");
		this.add(header);

		ReturnToMainMenuButton rtmm = new ReturnToMainMenuButton();
		this.add(rtmm);

		JLabel lblEmail = new JLabel("E-mail");
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblEmail.setBounds(17, 49, 110, 25);
		add(lblEmail);

		JLabel lblFirstName = new JLabel("First name");
		lblFirstName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblFirstName.setBounds(17, 85, 110, 25);
		add(lblFirstName);

		JLabel lblLastName = new JLabel("Last name");
		lblLastName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblLastName.setBounds(17, 121, 110, 25);
		add(lblLastName);

		JLabel lblBalance = new JLabel("Balance");
		lblBalance.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblBalance.setBounds(17, 157, 110, 25);
		add(lblBalance);

		JLabel lblID = new JLabel("User ID");
		lblID.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblID.setBounds(17, 193, 110, 25);
		add(lblID);

		lblEmailValue = new JLabel("");
		lblEmailValue.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmailValue.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEmailValue.setBounds(137, 49, 165, 25);
		add(lblEmailValue);

		lblBalanceValue = new JLabel("");
		lblBalanceValue.setHorizontalAlignment(SwingConstants.CENTER);
		lblBalanceValue.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblBalanceValue.setBounds(137, 157, 165, 25);
		add(lblBalanceValue);

		lblUserIDValue = new JLabel("");
		lblUserIDValue.setHorizontalAlignment(SwingConstants.CENTER);
		lblUserIDValue.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblUserIDValue.setBounds(137, 193, 165, 25);
		add(lblUserIDValue);

		textFieldFirstNameValue = new JTextField();
		textFieldFirstNameValue.setHorizontalAlignment(SwingConstants.CENTER);
		textFieldFirstNameValue.setColumns(10);
		textFieldFirstNameValue.setBounds(137, 85, 165, 25);
		add(textFieldFirstNameValue);

		textFieldLastNameValue = new JTextField();
		textFieldLastNameValue.setHorizontalAlignment(SwingConstants.CENTER);
		textFieldLastNameValue.setColumns(10);
		textFieldLastNameValue.setBounds(137, 121, 165, 25);
		add(textFieldLastNameValue);

		btnRefresh = new JButton("Refresh");
		btnRefresh.setBounds(33, 263, 110, 25);
		btnRefresh.addActionListener(new BtnRefreshSelf());
		add(btnRefresh);

		btnUpdate = new JButton("Update");
		btnUpdate.setBounds(165, 263, 110, 25);
		btnUpdate.addActionListener(new BtnUpdateSelf());
		add(btnUpdate);

	}

/**
 * Clears all the field in the panel and enables the buttons. 
 */
	@Override
	public void clearFields() {
		feedbackLabel.setText("");
		textFieldLastNameValue.setText("");
		textFieldFirstNameValue.setText("");
		lblUserIDValue.setText("");
		lblBalanceValue.setText("");
		lblEmailValue.setText("");
		enableButtons(true);
	}

	/**
	 * Creates a new Runnable of the type GetSelfInfoRunnable, sets it to the current runnable for the panel, places it in its own thread and runs it.
	 */
	public void getSelfInfo() {
		runnable = new GetSelfInfoRunnable();
		Thread t = new Thread(runnable);
		t.start();
	}

	/**
	 * Creates a new Runnable of the type UpdateSelfRunnable, sets it to the current runnable for the panel, places it in its own thread and runs it.
	 */
	public void updateSelf() {
		runnable = new UpdateSelfRunnable();
		Thread t = new Thread(runnable);
		t.start();
	}

	/**
	 * Enables or disables the buttons based on the parameter specified.
	 * @param value
	 * If true enables the buttons, if false, disables the buttons.
	 */
	private void enableButtons(boolean value) {
		if (value) {
			btnRefresh.setEnabled(true);
			btnUpdate.setEnabled(true);
		} else {
			btnRefresh.setEnabled(false);
			btnUpdate.setEnabled(false);
		}

	}

	private class BtnUpdateSelf implements ActionListener {
		
		public void actionPerformed(ActionEvent arg0) {
			updateSelf();
		}

	}

	private class BtnRefreshSelf implements ActionListener {
		
		public void actionPerformed(ActionEvent arg0) {
			getSelfInfo();
		}

	}

	/**
	 * See the run-method of this class for details.
	 * 
	 */
	private class GetSelfInfoRunnable extends BaseRunnableBCBS {
		/**
		 * Creates SQLreader and gets account information from database. 
		 */
		public void run() {
			SQLReader reader = new SQLReader();
			enableButtons(false);
			feedback("Loading....", true);
			if (!stop) {
				try {
					Account self = reader.getAccount(UI.MainFrame.currentUser
							.getId());
					if (!stop) {
						clearFeedback();
						lblBalanceValue.setText(String.valueOf(self
								.getBalance()));
						lblEmailValue.setText(String.valueOf(self.getEmail()));
						lblUserIDValue.setText(String.valueOf(self.getId()));
						textFieldFirstNameValue.setText(String.valueOf(self
								.getFirstname()));
						textFieldLastNameValue.setText(String.valueOf(self
								.getLastname()));
						enableButtons(true);
					}
				} catch (SQLException e) {
					if (!stop) {
						enableButtons(true);
						feedback("Connection error! Try again!", false);
					}
				}
			}
		}

	}
	
	/**
	 * See the run-method of this class for details.
	 * 
	 */
	private class UpdateSelfRunnable extends BaseRunnableBCBS {
		/**
		 * Updates the first and last name of the currently logged in user. Writes the updates to the SQL database.
		 */
		public void run() {
			SQLReader reader = new SQLReader();
			enableButtons(false);
			feedback("Updating....", true);

			String firstname = textFieldFirstNameValue.getText().trim();
			String lastname = textFieldLastNameValue.getText().trim();
			try {
				reader.updateAccount(UI.MainFrame.currentUser.getId(),
						firstname, lastname);

				UI.MainFrame.currentUser.setFirstname(firstname);
				UI.MainFrame.currentUser.setLastname(lastname);
				if (!stop) {
					getSelfInfo();
				}

			} catch (SQLException e) {
				if (!stop) {
					enableButtons(true);
					feedback("Connection error! Try again!", false);
				}
			}
		}

	}
}
